#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <unistd.h>
#include <fcntl.h>
//工具类

//设置一个sock成为非阻塞
void SetNonBlock(int sock)
{
    // F_GETFL 获取 sock的文件状态标记符
    int fl = fcntl(sock, F_GETFL);
    if (fl < 0)
    {
        std::cerr << "fcntl failed" << std::endl;
        return;
    }
    //获取成功后，将sock的文件状态设置为非阻塞，添加O_NONBLOCK
    fcntl(sock, F_SETFL, fl | O_NONBLOCK);
}
//分割字符串
void SplitSegment(std::string &inbuffer, std::vector<std::string> &tokens, std::string sep)
{
    while (true)
    {
        std::cout << "inbuffer: " << inbuffer << std::endl;
        auto pos = inbuffer.find(sep);
        if (pos == std::string::npos)
        {
            break; //后面没有分隔符了，说明剩下的不够一个有效报文
        }
        std::string sub = inbuffer.substr(0, pos); //[)
        tokens.push_back(sub);
        inbuffer.erase(0, pos+sep.size());
    }
}
//和业务强相关,根据协议来进行反序列化
bool Deserialize(std::string &seg, std::string& out1, std::string& out2) 
{
    std::string op = "+";
    auto pos = seg.find(op);
    if(pos == std::string::npos)//没有分隔符
        return false;
    out1 = seg.substr(0, pos);
    out2 = seg.substr(pos+op.size());
    return true;
}